try:
    import ctypes
    var1 = ctypes.windll.kernel32.GetConsoleWindow()
    if var1 !=0:
        ctypes.windll.user32.ShowWindow(var1,0)
        ctypes.windll.kernel32.CloseHandle(var1,0)
except:
    pass

from subprocess import Popen
from threading import Thread
import socket
import sys
import os
try:
    from PyQt5.QtCore import QRegExp,Qt
except:
    os.system("pip3 install pyqt5")
    from PyQt5.QtCore import QRegExp,Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

global o_tag
o_tag = 1

def get_ip():
    global ip, ip_pre3

    conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    conn.settimeout(2)
    try:
        conn.connect(("223.5.5.5",53))
        ip = conn.getsockname()[0]
        ip_pre3 = '.'.join(ip.split('.')[:3])+'.'
        #print(ip_pre3)
    except:
        ip = ''
        ip_pre3 = ''
    conn.close()

class Win(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(200, 200, 400, 400)
        self.setWindowTitle('Tools')
        self.click1_o = ''

        self.text1 = QTextEdit()
        self.text2 = QTextEdit()
        self.btn1 = QPushButton('adb exec-out screencap -p > ')
        self.btn2 = QPushButton('adb connect ... :5555')
        self.btn3 = QPushButton('Clipboard Get ...')
        self.btn4 = QPushButton('Clipboard Cast ...')

        layout = QVBoxLayout()
        layout.addWidget(self.text1)
        layout.addWidget(self.btn1)
        layout.addWidget(self.btn2)
        layout.addWidget(self.btn3)
        layout.addWidget(self.btn4)
        layout.addWidget(self.text2)
        self.setLayout(layout)

        self.btn1.clicked.connect(self.click1)
        self.btn2.clicked.connect(self.click2)
        self.btn3.clicked.connect(self.click3)
        self.btn4.clicked.connect(self.click4)

    def click1(self):
        global o_tag
        #var1 = os.getcwd()
        #os.chdir(os.path.dirname(os.path.abspath(__file__)))
        c = "adb devices"
        o = '\n'+os.popen(c).read()
        c = "adb exec-out screencap -p > "+str(o_tag)+".png"
        os.system(c)
        if os.path.getsize(str(o_tag)+".png") == 0:
            self.click1_o = '执行失败 { '+c+' }\n'+self.click1_o
        else:
            self.click1_o = str(o_tag)+'.png 已检出\n'+self.click1_o
            o_tag += 1
        self.text2.setPlainText(self.click1_o+o)
        #os.chdir(var1)

    def click2(self):
        get_ip()
        global ip, ip_pre3

        i = self.text1.toPlainText()
        self.text2.setPlainText('')
        c = "adb devices"
        o = os.popen(c).read()
        try:
            i = str(int(i))
        except:
            o += '请输入整数地址'
            self.text2.setPlainText(o)
            return

        os.system("adb tcpip 5555")
        os.system("adb connect "+ip_pre3+i+":5555")
        c = "adb devices"
        o = os.popen(c).read()
        o += "请拔除有线连接"
        self.text2.setPlainText(o)

    def click3(self):
        get_ip()
        global ip, ip_pre3
        try:
            import clipboard
        except:
            os.system("pip3 install clipboard")
            import clipboard

        i = self.text1.toPlainText()
        ip_target = ip_pre3 + i
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((ip_target, 1079))
        except:
            o = '连接失败 '+ip_target
            self.text2.setPlainText(o)
            return
        
        get = s.recv(131072)
        get_all = get
        while get:
            get = s.recv(131072)
            get_all += get

        get_all = get_all.split(b'0x00')[1].decode("utf-8")
        o = '收到内容'
        try:
            clipboard.copy(get_all)
            o += '已存储到了剪贴板\n\n'
        except:
            o += '\n\n'
        self.text2.setPlainText(o + get_all)

    def click4(self):
        get_ip()
        global o_tag, ip, ip_pre3
        try:
            import clipboard
        except:
            os.system("pip3 install clipboard")
            import clipboard

        self.text1.setPlainText(clipboard.paste())
        self.text2.setPlainText(ip)
        Thread(target=self.click4_thread).start()

    def click4_thread(arg):
        try:
            import clipboard
        except:
            os.system("pip install clipboard")
            import clipboard

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
        s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        port = 1079
        s.bind(('', port))
        s.listen(10)
        c, addr = s.accept()

        import hashlib
        send_all = hashlib.md5(clipboard.paste().encode('utf-8')).hexdigest().encode('utf-8') + b'0x00' + clipboard.paste().encode('utf-8')
        c.send(send_all)
        print(send_all)
        s.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Win()
    form.show()
    sys.exit(app.exec_())